package com.se.db;

import com.se.common.UserInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import static com.se.common.Constant.GET_SQL_CONNECTION_FAILED;

public class DbUserInformation {
    private static volatile DbUserInformation instance;
    private static final Logger logger = LoggerFactory.getLogger(DbUserInformation.class);

    private DbUserInformation() {
    }

    public static DbUserInformation getInstance() {
        if(instance == null) {
            synchronized (DbUserInformation.class) {
                if(instance == null) {
                    instance = new DbUserInformation();
                }
            }
        }
        return instance;
    }

    public void addUserInformation(UserInformation information) throws SQLException {
        String sql = "INSERT INTO userInformation(username,ownGroup,memberGroup,AIList,friendList,friendRequest)" +
                "VALUES (?,?,?,?,?,?)";
        Connection conn = DbUtils.getConnection();

        if(conn == null) {
            throw new SQLException(GET_SQL_CONNECTION_FAILED);
        }

        try(PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, information.getUserName());
            ps.setString(2, information.getOwnGroupAsString());
            ps.setString(3, information.getMemberGroupAsString());
            ps.setString(4, information.getAIListAsString());
            ps.setString(5, information.getFriendListAsString());
            ps.setString(6, information.getFriendRequestAsString());
            ps.executeUpdate();
        }

        DbUtils.close(conn);
        logger.info("向数据库中添加了新的用户信息, 用户名为: {}", information.getUserName());
    }

    public UserInformation get(String username) throws SQLException {
        String sql = "SELECT username,ownGroup,memberGroup,AIList,friendList,friendRequest FROM" +
                " userInformation WHERE username=?";
        Connection conn = DbUtils.getConnection();
        UserInformation re = null;
        logger.info("试图从数据库中查询用户信息,用户名： {}", username);

        if(conn == null) {
            throw new SQLException(GET_SQL_CONNECTION_FAILED);
        }

        try(PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, username);
            try(ResultSet rs = ps.executeQuery()) {
                if(rs.next()) {
                    re = mapUserInformation(rs);
                }
            }
        }

        DbUtils.close(conn);
        if (re != null) {
            logger.info("从数据库中查询用户信息,用户名： {}", re.getUserName());
        }
        return re;
    }

    public void update(UserInformation user) throws SQLException {
        String sql = "UPDATE userInformation set username=?,ownGroup=?,memberGroup=?,AIList=?" +
                ",friendList=?,friendRequest=? WHERE username=?";
        Connection conn = DbUtils.getConnection();

        if(conn == null) {
            throw new SQLException(GET_SQL_CONNECTION_FAILED);
        }

        try(PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, user.getUserName());
            ps.setString(2, user.getOwnGroupAsString());
            ps.setString(3, user.getMemberGroupAsString());
            ps.setString(4, user.getAIListAsString());
            ps.setString(5, user.getFriendListAsString());
            ps.setString(6, user.getFriendRequestAsString());
            ps.setString(7, user.getUserName());
            ps.executeUpdate();
        }

        DbUtils.close(conn);
        logger.info("更新了用户信息,用户名为: {}", user.getUserName());
    }

    public void delete(String username) throws SQLException{
        String sql = "DELETE FROM userInformation WHERE username=?";
        Connection conn = DbUtils.getConnection();

        if(conn == null) {
            throw new SQLException(GET_SQL_CONNECTION_FAILED);
        }

        try(PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, username);
            ps.executeUpdate();
        }

        DbUtils.close(conn);
        logger.info("从数据库中删除了用户名为: {} 的用户", username);
    }

    public UserInformation mapUserInformation(ResultSet rs) throws SQLException{
        UserInformation userInformation = new UserInformation();
        userInformation.setUserName(rs.getString("username"));
        userInformation.setOwnGroupByString(rs.getString("ownGroup"));
        userInformation.setMemberGroupByString(rs.getString("memberGroup"));
        userInformation.setAIListByString(rs.getString("AIList"));
        userInformation.setFriendListByString(rs.getString("friendList"));
        userInformation.setFriendRequestByString(rs.getString("friendRequest"));
        return userInformation;
    }
}
